//
//  HXNewPersonVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/29.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXNewPersonVC.h"
#import "HX_Macro.h"
#import "HXNewPersonCell.h"
#import "HXNewPersonCCell.h"
#import "HXlogosModel.h"
#import "HXNewPlayerModel.h"
#define kTableView @"reuseTabel"
#define kCollectionView @"reuseCollection"
@interface HXNewPersonVC ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HXNewPersonCCell *cell;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *lable;
@property (nonatomic, strong) NSMutableArray *lableCell;
/**
 *  头视图滚动
 */
@property (nonatomic, strong) NSMutableArray *logos;
/**
 *  播放的数组源
 */
@property (nonatomic, strong) NSMutableArray *player;

/**
 *  参数type
 */
@property (nonatomic, assign) NSInteger type;
/**
 *  参数ID
 */
@property (nonatomic, copy) NSString *ID;
/**
 *  count
 */
@property (nonatomic, copy) NSString *total;
/**
 *  当前的page数量
 */
@property (nonatomic, assign) NSInteger currentPage;

@property (nonatomic, assign) NSInteger countSele;
@end


@implementation HXNewPersonVC
- (NSMutableArray *)logos {
    if (!_logos) {
        _logos = [NSMutableArray array];
    }
    return _logos;
}
- (NSMutableArray *)player {
    if (!_player) {
        _player = [NSMutableArray array];
    }
    return _player;
}

- (NSMutableArray *)lableCell {
    if (!_lableCell) {
        _lableCell = [NSMutableArray array];
    }
    return _lableCell;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"新手入门";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self getNetWorking];
    [self creatTableView];
    [self creatCollectionView];
//    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
//    right.HX_height = 40;
//    right.HX_width = 40;
//    right.backgroundColor = [UIColor redColor];
//    [right setTitle:@"按钮" forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}
- (void)getNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=TourLogo&version=4.4&_time=1474428909&_signature=16d4b0efe4379ed8aa65c0b71f05ef39";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *logos = dicData[@"logos"];
        for (NSDictionary *dic in logos) {
            HXlogosModel *logos = [HXlogosModel ModelWithDic:dic];
            [self.logos addObject:logos];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)getNetWorkingPlayer:(NSString *)ID
                       type:(NSInteger)type {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=TourIndex&version=4.4&page=1&size=10&type=%ld&catid=%@&_time=1474428909&_signature=16d4b0efe4379ed8aa65c0b71f05ef39", type, ID];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
            HXNewPlayerModel *modle = [HXNewPlayerModel ModelWithDic:dic];
            [self.player addObject:modle];
        }
        self.total = dicData[@"total"];
        self.currentPage = [dicData[@"page"] integerValue];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 1;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH - 40, 110) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"HXNewPersonCCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionView];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, SCREEN_HEIGHT - 174) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"HXNewPersonCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.player.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = @"tableView";
    HXNewPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HXNewPersonCell" owner:nil options:nil];;
        cell = [nibs lastObject];
    }
    HXNewPlayerModel *model = self.player[indexPath.row];
    cell.newsPlayerModel = model;
    if (indexPath.row == self.player.count- 1) {
        NSInteger total = [self.total integerValue];
        total = total - self.currentPage * 10;
        if (total <= 0) {
            [cell.label setTitle:@"已显示全部" forState:UIControlStateNormal];
            cell.label.enabled = NO;
        } else {
            [cell.label setTitle:[NSString stringWithFormat:@"还剩下%ld篇" ,total] forState:UIControlStateNormal];
            [cell.label setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            cell.label.backgroundColor = [UIColor lightGrayColor];
            [cell.label addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return cell;
}
- (void)addBtn:(UIButton *)btn {
    self.currentPage += 1;
    [self addGetNetWorkingCurrentPage:self.currentPage];
}
- (void)addGetNetWorkingCurrentPage:(NSInteger)currentPage {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=TourIndex&version=4.4&page=%ld&size=10&type=%ld&catid=%@&user_id=1666223&_time=1473122952&_signature=a8a0c40d4f2d8ae0602d588da1550ff1",currentPage ,self.type, self.ID];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
            HXNewPlayerModel *modle = [HXNewPlayerModel ModelWithDic:dic];
            [self.player addObject:modle];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.player.count - 1) {
        return 275;
    } else {
        return 230;
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _logos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    _cell.backgroundColor = [UIColor whiteColor];
    HXlogosModel *log = self.logos[indexPath.row];
    _cell.logosModel = self.logos[indexPath.row];
    if (self.countSele == indexPath.row) {
        self.cell.name.textColor = [UIColor orangeColor];
    if (indexPath.row == 0) {
        self.ID = log.ID;
        self.type = log.type;
        [self getNetWorkingPlayer:self.ID type:self.type];
    }
    } else {
        self.cell.name.textColor = [UIColor lightGrayColor];
    }
    return _cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.player removeAllObjects];
    self.countSele = indexPath.row;
    HXlogosModel *log = self.logos[indexPath.row];
    self.type = log.type;
    self.ID = log.ID;
    [self getNetWorkingPlayer:self.ID type:self.type];
    [_collectionView reloadData];
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.collectionView.HX_width - 30) / 4, self.collectionView.HX_height);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
